package unipi.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import unipi.database.Tasks;
import unipi.helpers.HTMLPages;
import unipi.helpers.LoginCookie;

public class TaskDetails extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	public TaskDetails()
	{
		super();
	}
	
	public void init() throws ServletException
	{
		
	
	}
	
	private String getPage(String uid, String taskid)
	{
		String content = "<h1>Στοιχεία Εργασίας</h1>";

		Map<String, String> task = Tasks.get(taskid);		
			
		if(task == null)
			content += "Η εργασία αυτή δεν υπάρχει.";
		else
		{
			content += "<table>";
			content += "<tr><td>Τίτλος: </td><td>" + task.get("title") + "</td></tr>";
			content += "<tr><td>Περιγραφή: </td><td>" + task.get("description") + "</td></tr>";
			content += "<tr><td>Ομάδα: </td><td>" + task.get("group") + "</td></tr>";
			content += "<tr><td>Απαιτούμενες Ώρες: </td><td>" + task.get("hoursRequired") + "</td></tr>";
			content += "<tr><td>Ποσοστό Ολοκλήρωσης: </td><td>" + task.get("percentCompleted") + "</td></tr>";
			content += "<tr><td>Ημερομηνία Ανάθεσης: </td><td>" + task.get("assignedDate") + "</td></tr>";
			content += "<tr><td>Προθεσμία: </td><td>" + task.get("dueDate") + "</td></tr>";
			content += "<tr><td>Ημερομηνία Έναρξης: </td><td>" + task.get("startDate") + "</td></tr>";
			content += "<tr><td>Ξεκίνησε στις: </td><td>" + task.get("startedDate") + "</td></tr>";
			content += "<tr><td>Ανάθεση από: </td><td>" +
							"<a class=\"genericLink\" href=\"Profile?id=" + task.get("assignerID") + "\">"+
							task.get("assignedBy") + "</a></td></tr>";
			
			String taskUsers = Tasks.getTaskUsers(taskid);
			
			if(taskUsers != null)
				content += "<tr><td>Αναθεση στους: </td><td>" + taskUsers + "</td></tr>";
			content += "</table>";	
			
			content += "<a class=\"genericLink\" href=\"Blog?uid="+uid+"&taskid=" + taskid + "\">Μετάβαση στο Ιστολόγιο</a><br/>";
			if(Tasks.isAssigned(uid, taskid))
			{
				content += "<a class=\"genericLink\" href=\"TaskProgress?id=" + taskid + "\">Προσθήκη Προόδου</a>";
			}
		}
		
		return HTMLPages.getGeneralPage(content, uid);	
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];

			request.setCharacterEncoding("utf-8");
			String taskid = request.getParameter("id");
			
			response.setContentType("text/html; charset=UTF-8");
			
			PrintWriter out = response.getWriter();
			
			out.println(getPage(uid, taskid));
			
			out.close();				
		
		}
	}
	
}
